﻿@using Resources;
@using HP.HPTRIM.Web.Configuration;
@using HP.HPTRIM.ServiceModel;
@using System.Linq;

@if (Model != null && Model.Results != null && Model.Results.Count > 0)
{



    int progressiveCount = 0;

    Int32.TryParse(HttpContext.Current.Request.QueryString["start"], out progressiveCount);

    if (progressiveCount > 0)
    {
        progressiveCount = progressiveCount - 1;
    }


    progressiveCount = progressiveCount + Model.Results.Count;

    int pagingLength = 4;
    int pagingHalfWay = (int)Math.Ceiling((double)pagingLength / 2); ;

    int pageSize = 0;

    if (Int32.TryParse(HttpContext.Current.Request.QueryString["pageSize"], out pageSize) == false)
    {
        pageSize = 15;
    }

    long pageNo = (int)Math.Ceiling((double)progressiveCount / pageSize);
    long totalPages = (Model.TotalResults + pageSize - 1) / pageSize;
    List<long> pages = new List<long>();

    int counter = 1;
    while (pages.Count < pagingLength && pages.Count < totalPages)
    {
        if (pageNo > pagingHalfWay && pageNo <= (totalPages - pagingHalfWay))
        {
            pages.Add(counter + pageNo - pagingHalfWay);
        }
        else if (pageNo > pagingHalfWay)
        {
            pages.Add((totalPages - pagingLength) + counter);
        }
        else
        {
            pages.Add(counter);
        }
        counter++;
    }
    <h3 id="search-title">@Model.SearchTitle</h3>
    <table class="table trim-list search-results">
        <thead>
            <tr>
                @foreach (PropertyOrFieldDef pfdef in Model.PropertiesAndFields[this.TrimHelper.TrimType])
                {
                    if (pfdef.Id != "RecordExtension")
                    {
                        if (pfdef.Id != "RecordRecordType")
                        {
                            <th>@pfdef.Caption</th>
                        }
                        else
                        {
                            <th class="row-icon"></th>
                        }
                    }
                }
            </tr>
        </thead>
        <tbody>
            @foreach (TrimObject record in Model.Results)
            {
                <tr trim-uri="@record.Uri" data-trim-type="@Model.TrimType" class="btn-link">
                    @foreach (PropertyOrFieldDef pfdef in Model.PropertiesAndFields[this.TrimHelper.TrimType])
                    {
                        if (pfdef.Id != "RecordExtension")
                        {
                            if (pfdef.Id == "RecordRecordType")
                            {
                                <td class="row-icon">
                                    <img src="images/record_types/@((record as Record).RecordType.Icon.Id).png" />
                                    <div>@((record as Record).Extension)</div>
                                </td>
                            }
                            else if (pfdef.PFFormat == PropertyOrFieldFormat.Datetime)
                            {
                                <td class="nowrap">@record.GetPropertyOrFieldString(pfdef.Id)</td>
                            }
                            else
                            {
                                <td class="prop-val">@record.GetPropertyOrFieldString(pfdef.Id)</td>
                            }
                        }
                    }
                </tr>
            }
        </tbody>
    </table>




    if (this.Model.TotalResults > 0)
    {
        <div class="pagination pagination-centered">
            <ul id="trim-pagination">
                @{
        var queryString = new System.Collections.Specialized.NameValueCollection(HttpContext.Current.Request.QueryString);
        if ((progressiveCount - 1) > Model.Results.Count)
        {
            queryString["pageSize"] = pageSize.ToString();
            queryString["start"] = "1";
            <li>
                <a href="?@queryString.ToFormUrlEncoded()">&laquo;</a>
            </li>
            queryString["start"] = (progressiveCount - (pageSize * 2)).ToString();
            <li>
                <a href="?@queryString.ToFormUrlEncoded()">&lt;</a>
            </li>

        }
        else
        {
            <li class="disabled"><a href="#">&laquo;</a></li>
            <li class="disabled"><a href="#">&lt;</a></li>
        }

        foreach (long pageInSet in pages)
        {
            queryString["pageSize"] = pageSize.ToString();
            queryString["start"] = (((pageInSet - 1) * pageSize) + 1).ToString();

            <li class="@(pageInSet == ((pageNo > 1) ? pageNo : pageNo) ? "active" : "")">
                <a href="?@queryString.ToFormUrlEncoded()">@pageInSet</a>
            </li>
        }

        if (progressiveCount < Model.TotalResults)
        {
            queryString["pageSize"] = pageSize.ToString();
            queryString["start"] = (progressiveCount == pageSize ? progressiveCount + 1 : progressiveCount).ToString();
            <li>
                <a href="?@queryString.ToFormUrlEncoded()">></a>
            </li>
            queryString["start"] = (totalPages * (pageSize-1)).ToString();
            <li>
                <a href="?@queryString.ToFormUrlEncoded()">&raquo;</a>
            </li>
        }
        else
        {
            <li class="disabled"><a href="#">></a></li>
    <li class="disabled"><a href="#">&raquo;</a></li>
        }
}
            </ul>
        </div>
    }
    else
    {
        int start = 0;
        Int32.TryParse(HttpContext.Current.Request.QueryString["start"], out start);

        var queryString = new System.Collections.Specialized.NameValueCollection(HttpContext.Current.Request.QueryString);

        <div class="pagination pagination-centered">
            <ul id="trim-pagination">
                <li class="@(start < 2 ? "disabled" : null)">
                    @{
        queryString["start"] = (start > pageSize ? start - pageSize : 0).ToString();
        <a href="?@queryString.ToFormUrlEncoded()">&lt;</a>
                    }
                </li>

                <li class="@(!this.Model.HasMoreItems ? "disabled" : null)">
                    @if (this.Model.HasMoreItems)
                    {
                        start = (start == 0 ? start + 1 : start);
                        queryString["start"] = (start + pageSize).ToString();
                        <a href="?@queryString.ToFormUrlEncoded()">></a>
                    }
                    else
                    {
                        <span>&raquo;</span>
                    }
                </li>
            </ul>
        </div>
    }
}
else
{

    <div class="alert alert-block">
        @if (this.Model.ResponseStatus != null && this.Model.ResponseStatus.Message != null)
        {
            <h4>@this.Model.ResponseStatus.Message</h4>

        }
        else
        {
            <h3>@Model.SearchTitle</h3>
            @Translations.lang.warning_no_matching
        }

    </div>
}

